 package division;
import java.util.Scanner;
/**
 * Деление в столбик
 * @author Evgeny Pavlenko
 *
 */
public class LongDivision {
	final static int BASE=10;
	
	/**
	 * Деление в столбик
	 * двух целых чисел
	 * @return строка с результатом
	 */
	public static String divide(int dividend, int divisor)
	{
		int sign=getSign(dividend,divisor);
		dividend=Math.abs(dividend);
		divisor=Math.abs(divisor);
		if(dividend==divisor)
		{
			return "\nQuotient="+1+"\nModule="+0;
		}
	    if(dividend>divisor)
		{
	    	return divide(dividend,divisor, sign);	    	
		}
	    else
	    {
	    	if(sign==2){dividend*=-1;}
			return "\nQuotient="+0+"\nRemainder="+dividend;
	    }
	    
	}
	/**
	 * 
	 * @return знак результата
	 */
	private static int getSign(int dividend,int divisor)
	{
		if(dividend<0&&divisor<0) 
			return 1;
	    if(dividend<0&&divisor>0) 
	    	return 2;
	    if(dividend>=0&&divisor<0)
	    	return 3;
		return 0;
	  }
	
	private static int nextNum(int dividend, int index)
	{
		return  (int)(dividend* Math.pow(BASE, index));
	}
	
	private static String divide(int dividend, int divisor, int sign)
	{
		int dd=digitsCount(dividend); 
		int dv=digitsCount(divisor); 
		String print=String.format("%s","Long division:\n");
		print+=String.format("%d|%d\n", dividend,divisor);
		int remainder=0;
		int quotient=0;
		int spacesCount=0;
		for (int i=0;i<=dd-dv;i++,spacesCount++){
			if(i==0)
				remainder=nextNum(dividend,dv-dd+i);
			else
			remainder=remainder*BASE + (nextNum(dividend,dv-dd+i)-nextNum(dividend,dv-dd+i-1)*BASE);
			if(remainder>=divisor){
				int quotientSteps=remainder/divisor;
				quotient=quotient*BASE+quotientSteps;
				print+=spaces(spacesCount);
				print+=String.format("%d\n", remainder);
				print+=spaces(spacesCount);
				print+=String.format("%d\n", divisor*quotientSteps);
				print+=spaces(spacesCount);
				print+="___\n";
				remainder-=divisor*quotientSteps;
				}
			}
		print+=spaces(spacesCount);
		print+=String.format("%d\n", remainder);
		if(sign==1){remainder*=-1;}
		if(sign==2){quotient*=-1;remainder*=-1;}
		if(sign==3){quotient*=-1;}
		print+=String.format("Quotient=%d\n",quotient);
		print+=String.format("Module=%d\n",remainder);
		return print;
		}
	private static String spaces(int n)
	{
		String string="";
		for(int i=0;i<n;i++)
			string+=" ";
		return string;
	}
	public static int digitsCount(int number)
	{
	 return (int) (Math.floor(Math.log10(number))+1);
	}
	/**
	 * 
	 * @param string указывает какое именно число нужно ввести
	 * @return число с потока ввода
	 */
	public static int scan(Scanner scanner, String string)
	{
		System.out.println(string);
		return scanner.nextInt();		
	}	
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int dividend=0;
		int divisor=0;
		do{
			dividend=scan(scanner,"Enter the dividend:");
		    divisor=scan(scanner,"Enter the divisor:");
			System.out.println(divide(dividend,divisor));
			System.out.println("Again? y/n");
			} while (scanner.next().equalsIgnoreCase("y"));
		scanner.close();
		}
	}
